package com.mtt.erp.goods.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.mtt.erp.common.annotation.Log;
import com.mtt.erp.common.controller.BaseController;
import com.mtt.erp.common.domain.MttErpResponse;
import com.mtt.erp.common.domain.QueryRequest;
import com.mtt.erp.common.exception.MttErpException;
import com.mtt.erp.goods.domain.PGoods;
import com.mtt.erp.goods.dto.PGoodsDto;
import com.mtt.erp.goods.service.IPGoodsService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @Auther: SJZ    2907374717@qq.com
 * @Date: 2019/5/22 11:24
 * @Description: 商品Controller
 */
@Slf4j
@RestController
@RequestMapping("/goods")
@ApiIgnore
public class PGoodsController extends BaseController {

    private String message;
    @Autowired
    private IPGoodsService iPGoodsService;

    @Log("查询商品")
    @GetMapping
    @RequiresPermissions("goods:view")
    public Map<String, Object> goodsList(QueryRequest request, PGoodsDto pGoodsDto) {
        return getDataTable(this.iPGoodsService.findGoods(request, pGoodsDto));
    }


    //@Log("添加商品")
    @PostMapping
    @RequiresPermissions("goods:add")
    public void addGoods(@Valid PGoods goods) throws MttErpException {
        try {
            this.iPGoodsService.createGoods(goods);
        } catch (Exception e) {
            message = "新增商品失败";
            log.error(message);
            throw new MttErpException(message);
        }

    }

    // @Log("修改商品")
    @PutMapping
    @RequiresPermissions("goods:update")
    public void updateGoods(@Valid PGoods goods) throws MttErpException {
        try {
            this.iPGoodsService.updateGoods(goods);
        } catch (Exception e) {
            message = "修改商品失败";
            log.error(message, e);
            throw new MttErpException(message);
        }

    }


    //商品上架
    @PutMapping("putAway")
    @RequiresPermissions("goods:putAway")
    public void putAway(@NotBlank(message = "{required}") String ids) throws MttErpException {
        String[] goodsIds = ids.split(StringPool.COMMA);
        try {
            this.iPGoodsService.putAway(goodsIds);
        } catch (Exception e) {
            log.error(message, e);
            throw new MttErpException(message);
        }
    }


    //商品下架
    @PutMapping("/soldOut")
    @RequiresPermissions("goods:soldOut")
    public void soldOut(@NotBlank(message = "{required}") String ids) throws MttErpException {
        String[] goodsIds = ids.split(StringPool.COMMA);
        try {
            this.iPGoodsService.saleOut(goodsIds);
        } catch (Exception e) {
            log.error(message, e);
            throw new MttErpException(message);
        }
    }


    @PostMapping("excel")
    @RequiresPermissions("goods:excel")
    public void export(PGoodsDto goodsDto, QueryRequest request, HttpServletResponse response) throws MttErpException {
        try {
            List<PGoods> list = this.iPGoodsService.findGoodsList(request, goodsDto);
            ExcelKit.$Export(PGoods.class, response).downXlsx(list, false);

        } catch (Exception e) {
            message = "导出Excel失败";
            log.error(message, e);
            throw new MttErpException(message);
        }
    }


    @PostMapping("import")
    public MttErpResponse importByExcel(@RequestParam("file") MultipartFile file) throws MttErpException {
        try {
            return this.iPGoodsService.improtByExcel(file);
        } catch (Exception e) {
            message = "导入Excel数据失败," + e.getMessage();
            log.error(message);
            throw new MttErpException(message);
        }
    }




}
